package fhp.hlhj.giantfold.activity.login

import android.content.Intent
import android.widget.Toast
import com.lzy.okgo.model.HttpParams
import fhp.hlhj.giantfold.BaseActivity
import fhp.hlhj.giantfold.Contents
import fhp.hlhj.giantfold.MainActivity
import fhp.hlhj.giantfold.R
import fhp.hlhj.giantfold.event.RecreateEvent
import fhp.hlhj.giantfold.interfaces.IPhoneLogin
import fhp.hlhj.giantfold.event.RefreshLoginEvent
import fhp.hlhj.giantfold.javaBean.PhoneLoginBean
import fhp.hlhj.giantfold.presenter.PhoneLoginPresenter
import fhp.hlhj.giantfold.utils.LogUtil
import fhp.hlhj.giantfold.utils.SPUtils
import fhp.hlhj.giantfold.utils.ToastUtils
import kotlinx.android.synthetic.main.activity_login_aty.*
import kotlinx.android.synthetic.main.gain_pop.*
import org.greenrobot.eventbus.EventBus

class LoginAty : BaseActivity(), IPhoneLogin {
    private lateinit var phoneLoginPresenter:PhoneLoginPresenter
    override fun getContentId(): Int = R.layout.activity_login_aty
    override fun initListener() {
        btForget.setOnClickListener {
            startActivity(Intent(this, ForgetAty::class.java))
        }
        btExit.setOnClickListener {
            finish()
        }
        btRegis.setOnClickListener {
            startActivity(Intent(this, PhoneRegisAty::class.java))
        }
        btCommit.setOnClickListener {
            val params=HttpParams()
            params.put("user_name",etPhone.text.toString())
            params.put("password",etPwd.text.toString())
            params.put("client","android")
            phoneLoginPresenter.login(params)
        }
    }
    override fun initView() {
        phoneLoginPresenter=PhoneLoginPresenter(this);
    }
    override fun showLoading() {
        loadingDialog.show()
    }
    override fun login(phoneLoginBean: PhoneLoginBean) {
        loadingDialog.dismiss()
        if (phoneLoginBean.code==200){
            Toast.makeText(this,"登录成功",Toast.LENGTH_SHORT).show()
            Contents.isLogin=true
            Contents.userInfo=phoneLoginBean.data
            Contents.userId=phoneLoginBean.data.id
            EventBus.getDefault().postSticky(RefreshLoginEvent())
            EventBus.getDefault().postSticky(RecreateEvent())
            SPUtils.saveUserIsLogin(this,true)
            SPUtils.saveUser(this,etPhone.text.toString(),etPwd.text.toString())
            SPUtils.saveLoginType(this,0)
            setResult(-1)
            finish()
        }else{
            ToastUtils.toast(this,phoneLoginBean.msg)
        }
    }
}
